<template>
  <div class="search">
    <div class="search-box">
      <div class="search-box-left" @click="noCancel">
        <van-icon name="arrow-left" size="20" />
      </div>
      <div class="search-box-center">
        <van-search
          v-model="value"
          shape="round"
          clearable
          @search="onSearch"
          @input="onInput"
        />
      </div>
      <div class="search-box-right" @click="onSearch">搜索</div>
    </div>
    <div class="content" v-if="value == ''">
      <div class="history" v-if="history.length > 0">
        <div class="history-top">
          <div class="history-title">历史搜索</div>
          <div class="delete" @click="onDelete">
            <van-icon name="delete-o" size="16" />
          </div>
        </div>
        <div class="history-bottom">
          <div
            class="history-item"
            v-for="(item, index) in history"
            :key="index"
            @click="onSearch1(item)"
          >
            {{ item }}
          </div>
        </div>
      </div>
      <div class="hot" v-if="hot.length > 0">
        <div class="hot-title">热门搜索</div>
        <div class="hot-item-box">
          <div
            class="hot-item"
            v-for="(item1, index1) in hot"
            :key="index1"
            @click="onSearch1(item1.name)"
          >
            {{ item1.name }}
          </div>
        </div>
      </div>
    </div>
    <div class="advice" v-if="value != ''">
      <div
        class="advice-item"
        v-for="(item2, index2) in advice"
        :key="index2"
        @click="onSearch1(item2.name)"
      >
        {{ item2.name }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Search",
  data() {
    return {
      value: "",
      history: [],
      hot: [],
      advice: [],
    };
  },
  created() {
    this.getHot();
    this.getHistory();
  },
  methods: {
    noCancel() {
      this.$router.go(-1);
    },
    getHistory() {
      if (localStorage.getItem("history")) {
        this.history = localStorage.getItem("history").split(",");
      }
    },
    getHot() {
      this.axios({
        method: "get",
        url: "recipe/search/hot",
      }).then((result) => {
        
        if (result.data.state == "success") {
          this.hot = result.data.result.suggests;
        }
      });
    },
    onSearch() {
      if(this.value == ''){
        this.$toast('请输入关键字')
        return
      }
      this.$router.push({name:'SearchInfo',query:{value:this.value}})
      if (this.history.length == 10) {
        this.history.pop();
      }
      if (this.history.indexOf(this.value) != -1) {
        return;
      }
      this.history.unshift(this.value);
      localStorage.setItem("history", this.history);
    },
    onInput() {
      setTimeout(() => {
        if (this.value != "") {
          this.axios({
            method: "get",
            url: "recipe/search/suggests",
            params: {
              kw: this.value,
            },
          }).then((result) => {
            
            if (result.data.state == "success") {
              this.advice = result.data.result.suggests;
            }
          });
        }
      }, 200);
    },
    onSearch1(name) {
      this.value = name;
      this.onSearch();
    },
    onDelete() {
      this.history = [];
      localStorage.removeItem("history");
    },
  },
};
</script>

<style lang="scss" scoped>
.search {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
  .search-box {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 30px;
    background-color: #fbb705;
    .search-box-left {
      width: 30px;
      height: 54px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .search-box-center {
      width: calc(100% - 30px - 40px);
    }
    .van-search{
      background-color: #fbb705;
    }
    .search-box-right {
      width: 40px;
      font-size: 15px;
    }
  }
  .content {
    width: 100%;
    height: 100%;
    .history {
      width: calc(100% - 30px);
      padding: 15px;
      margin-top: 10px;
      .history-top {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
        .history-title {
          font-size: 15px;
        }
      }
      .history-bottom {
        display: flex;
        flex-wrap: wrap;
        .history-item {
          padding: 2px 10px;
          background-color: #eee;
          margin-right: 10px;
          margin-bottom: 5px;
          border-radius: 5px;
        }
      }
    }
    .hot {
      width: calc(100% - 30px);
      padding: 0 15px;
      .hot-title {
        font-size: 15px;
        margin-bottom: 10px;
      }
      .hot-item-box {
        display: flex;
        flex-wrap: wrap;
        .hot-item {
          padding: 2px 10px;
          background-color: #eee;
          margin-right: 10px;
          margin-bottom: 5px;
          border-radius: 5px;
        }
        .hot-item:nth-child(1),
        .hot-item:nth-child(2) {
          background-color: #fff;
          border: 1px solid rgb(252, 89, 89);
          color: rgb(252, 89, 89);
        }
      }
    }
  }
  .advice {
    width: calc(100% - 40px);
    padding: 0 20px;
    .advice-item {
      font-size: 16px;
      border-bottom: 1px solid #eee;
      padding: 10px 0;
    }
  }
}
</style>